import { create } from 'zustand';
import { queryAfterSalesOrderList } from './service';
import { fieldMap } from './fieldMap';

// 创建状态管理器
export const useScanRegistrationStore = create((set, get) => ({
  orderList: [],
  // 是否确认收货
  isConfirmReceivingGoods: false,
  // 是否有次品
  isDefectiveGoods: false,
  // 当前选中的商品 给删除用
  currentSelectGoods: {},
  isLoading: false,
  orderIdParameter: '', // 用户查询
  pageConfig: {
    monitorShoot: false, // 是否开启监控拍摄查询单号
    enlargeShowTime: undefined,	// 需要monitorShoot为true 快递单号录入时放大展示
    fillOpenPacketNumber: false,	// 填充拆包数量功能
  },
  queryData: {},
  queryAfterSalesOrderList: async (data) => {
    const { pageConfig } = get()
    set((state) => {
      return {
        ...state,
        isConfirmReceivingGoods: false,
        isLoading: true,
        queryData: data,
      }
    })
    
    return queryAfterSalesOrderList(data).then(res => {
      let orderList = Array.isArray(res.data) ? res.data : []
      const currentSelectGoods = {}
      orderList = orderList.map(item => {
        item[fieldMap['售后商品列表']] = item[fieldMap['售后商品列表']].map(obj => {
          obj[fieldMap['次品数量']] = 0
          obj[fieldMap['商品唯一码']] = obj[fieldMap['商品唯一码']].map(value => {
            return {
              value,
              status: 1, // 1 正常 2 次品
              selected: pageConfig.fillOpenPacketNumber
            }
          })
          
          if (pageConfig.fillOpenPacketNumber) {
            obj[fieldMap['拆包数量']] = obj[fieldMap['申请退货数量']] || 0
          }
          return obj
        })

        currentSelectGoods[item[fieldMap['售后单号']]] = item[fieldMap['售后商品列表']]
        return item
      })

      set((state) => {
        return {
          ...state,
          currentSelectGoods,
          orderList,
        }
      })

      console.log(res.data, 'queryAfterSalesOrderList')
      return orderList
    }).finally(() => {
      set((state) => {
        return {
          ...state,
          isLoading: false
        }
      })
    })
  },
  
  // 更新state
  updateState: (newVal) => {
    set((state) => {
      return {
        ...state,
        ...newVal,
      }
    });
  },

  resetState (newVal) {
    set((state) => {
      return {
        ...state,
        orderList: [],
        // 是否确认收货
        isConfirmReceivingGoods: false,
        // 是否有次品
        isDefectiveGoods: false,
        // 当前选中的商品 给删除用
        currentSelectGoods: {},
        isLoading: false,
        ...newVal,
      }
    });
  },
  
  updateAfterSalesIds: async () => {
    let { queryData, orderList } = get()
    queryAfterSalesOrderList(queryData).then(res => {
      const newListData = orderList.slice()
      let list = Array.isArray(res.data) ? res.data : []
      list.forEach((item, index) => {
        newListData[index][fieldMap['售后id']] = item[fieldMap['售后id']]
        newListData[index][fieldMap['售后单号']] = item[fieldMap['售后单号']]
      });

      set((state) => {
        return {
          ...state,
          orderList: newListData,
        }
      })

      console.log(orderList, 'updateAfterSalesIds')
      return orderList
    })
  }
}));



